from sqlalchemy import Column, BigInteger, DECIMAL, Integer, DateTime, Index, String, ForeignKey
from sqlalchemy.orm import relationship

from config.database import Base, BaseMixin


class BgAffiliateCommission(Base, BaseMixin):
    """推广返利记录表"""
    __tablename__ = 'bg_affiliate_commission'

    inviter_id = Column(BigInteger,  ForeignKey('sys_user.user_id'),  nullable=False, comment='推广者用户ID', index=True)
    invitee_id = Column(BigInteger,  ForeignKey('sys_user.user_id'), nullable=False, comment='被推广者用户ID', index=True)
    order_id = Column(BigInteger,  ForeignKey('bg_order.id'),  nullable=False, comment='订单ID', index=True)
    order_amount = Column(DECIMAL(10, 2), nullable=False, comment='订单金额')
    commission_rate = Column(DECIMAL(5, 2), nullable=False, comment='返利比例(%)')
    commission_amount = Column(DECIMAL(10, 2), nullable=False, comment='返利金额')
    status = Column(Integer, nullable=False, default=0, comment='状态(0:待结算 1:已结算 2:已取消)')
    settle_time = Column(DateTime, nullable=True, comment='结算时间')

    # 关联关系
    inviter = relationship("SysUser", foreign_keys=[inviter_id], back_populates="commission_records")
    invitee = relationship("SysUser", foreign_keys=[invitee_id])
    order = relationship("BgOrder", foreign_keys=[order_id], back_populates="commission_records")
